package com.mtons.mblog.modules.template.directive;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.base.utils.BeanMapUtils;
import com.mtons.mblog.modules.data.PostVO;
import com.mtons.mblog.modules.data.QuestVO;
import com.mtons.mblog.modules.service.QuestService;
import com.mtons.mblog.modules.template.DirectiveHandler;
import com.mtons.mblog.modules.template.TemplateDirective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

@Component
public class QuestContentsDirective extends TemplateDirective {

    @Autowired
    private QuestService questService;

    @Override
    public String getName() {
        return "questContents";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        String order = handler.getString("order", Consts.order.NEWEST);

        Pageable pageable = wrapPageable(handler, Sort.by(Sort.Direction.DESC, BeanMapUtils.postOrder(order)));
        Page<QuestVO> result = questService.paging(pageable);
        handler.put(RESULTS, result).render();
    }
}
